from django.shortcuts import render, get_object_or_404
from lpts.models import Job, Testclass, Testcontent, Value
from django.db.models import Q
from django.core.paginator import Paginator,EmptyPage, PageNotAnInteger
from lpts.forms import osform
from django.contrib.auth.decorators import login_required

# Create your views here.

@login_required(login_url='login')
def  index(request):
    '''Index '''
    return render(request, "index.html")

@login_required(login_url='login')
def reports(request):
    "report "
    
    num_pages=request.GET.get('page','1')
    query = request.GET.get('q',' ')
    if query:
        query = query.strip()
        qset = (
                Q(jobid__icontains=query) |
                Q(name__icontains=query) |
                Q(hardware__name__icontains=query) |
                Q(os__name__icontains=query)|
                Q(os__release__icontains=query)|
                Q(os__version__icontains=query)|
                Q(os__arch__name__icontains=query)|
                Q(scenegroup__name__icontains=query)|
                Q(tester__name__icontains=query)
                )
        Job_objects  = Job.objects.filter(qset)
    else:
        Job_objects = Job.objects.all()


        
    os_id = request.REQUEST.get("os", '')
    if os_id:
        #osfilterform = OSFilterForm(request.GET)
        osfilterform = osform(request, Job_objects,  method="GET")
        Job_objects = Job_objects.filter(os__pk=os_id)   
    else:
        #osfilterform = OSFilterForm()
        osfilterform = osform(request, Job_objects)
            
    Job_objects = Job_objects.order_by("-jobid")
    paginator = Paginator(Job_objects, 12)
    try:
        Job_objects =paginator.page(num_pages)
    except PageNotAnInteger:
        Job_objects = paginator.page(1)
    except EmptyPage:
        Job_objects =paginator.page(paginator.num_pages)
    
    return  render(request, "reports.html", {
                                                  "Job_objects":Job_objects,
                                                  "osfilterform":osfilterform,
                                                  "query":query})


@login_required(login_url='login')
def results(request, jobid):
    job_object = get_object_or_404(Job,  jobid=jobid)
    result_objects = job_object.result_set.all()
    testclass_objects = Testclass.objects.all()
    value_objects = Value.objects.all()
    return render(request, 'results.html',{
                                           'job_object':job_object,
                                          "result_objects":result_objects,
                                        "testclass_objects":testclass_objects,
                                        "value_objects":value_objects,
                                                                })

@login_required(login_url='login')
def  data_model(request):
    '''datamodel '''
    return render(request, "datamodel.html")
    
@login_required(login_url='login')
def  tpjg(request):
    '''tuo pu jie gou '''
    return render(request, "tpjg.html")

@login_required(login_url='login')
def  ywlc(request):
    '''ye wu liu cheng'''
    return render(request, "ywlc.html")